package com.sykj.qianxuezhan.controller;

import com.baidu.aip.ocr.AipOcr;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sykj.config.oss.FileUploadUtil;
import com.sykj.qianxuezhan.bean.Shop;
import com.sykj.qianxuezhan.bean.ShopPost;
import com.sykj.qianxuezhan.bean.ShopUser;
import com.sykj.qianxuezhan.server.ShopPostService;
import com.sykj.qianxuezhan.server.ShopService;
import com.sykj.qianxuezhan.server.ShopUserService;
import com.sykj.qianxuezhan.utils.AIP;
import com.sykj.qianxuezhan.utils.ChineseUtils;
import com.sykj.qianxuezhan.utils.JSONChange;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author QianXuezhan
 * @Date 2020/7/18 23:56
 * @Description: TODO
 */
@RestController
@CrossOrigin
@RequestMapping("/shop_user")
public class ShopUserController {


    //导入服务层接口
    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private FileUploadUtil fileUploadUtil;

    private ChineseUtils chineseUtils;

    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopPostService shopPostService;
    /**
     * @Date:
     * @Description: 查询店主信息
     **/
    @RequestMapping("/queryShopUserDz")
    public  List<ShopUser>  queryShopUserDz(ShopUser shopUser){
        shopUser.setUserType("1");
        shopUser.setIsDel("0");
        return shopUserService.queryShopUserDz(shopUser);
    }

    /**
     * @Date:
     * @Description:  添加店主信息
     **/
    @RequestMapping("/saveShopUser")
    public ResultBody saveShopUser(ShopUser shopUser,String remark22){
        shopUser.setRemark(remark22);
        String sex2=shopUser.getSex();
        if(sex2=="男" || sex2=="雄"){
            shopUser.setSex("1");
        }else if (sex2=="女" || sex2=="雌"){
            shopUser.setSex("2");
        }else{
            shopUser.setSex("3");
        }
        System.out.println(shopUser.getUsername()+"---"+shopUser.getAccount());
        //根据定时器生成id
        String shopUserId=Long.toString(System.currentTimeMillis());
        shopUser.setShopUserId(shopUserId);
        shopUser.setUserType("1");
        shopUser.setStatus("0");
        shopUser.setIsDel("0");
        shopUserService.saveShopUser(shopUser);
        return  new ResultBody(CommonEnum.SUCCESS);
    }

    /**
     * @Date:
     * @Description:   假删除店主信息
     **/
    @RequestMapping("/updateShopUserDelId")
    public ResultBody  updateShopUserDelId(String shopUserId){
        UpdateWrapper queryWrapper=new  UpdateWrapper();
        queryWrapper.eq("shop_user_id",shopUserId);
        queryWrapper.set("is_del",1);
        shopUserService.update(queryWrapper);
        return  new ResultBody(CommonEnum.SUCCESS);
    }


    /**
     * @Date:
     * @Description: 修改店主信息
     **/
    @RequestMapping("/updateShopUserId")
    public ResultBody  updateShopUserDelId(String shopUserId,String email,String mobile,String status,String remark){

        ShopUser shopUser=new ShopUser();
        shopUser.setShopUserId(shopUserId);
        shopUser.setEmail(email);
        shopUser.setMobile(mobile);
        shopUser.setStatus(status);
        shopUser.setRemark(remark);
        shopUserService.updateShopUserId(shopUser);
        return  new ResultBody(CommonEnum.SUCCESS);
    }



    /**
     * @Date:
     * @Description: 身份证识别
     **/
    @RequestMapping(value = "/idCard",method = RequestMethod.POST)
    public Map idCard(@RequestParam("file_idcard") MultipartFile[] fileArr) throws Exception{

        MultipartFile mf1 = fileArr[0];
       byte[] by1 = mf1.getBytes();
        AipOcr client1 = new AipOcr(AIP.APP_ID_TEXT,AIP.API_KEY_TEXT,AIP.SECRET_KEY_TEXT);
        JSONObject jo_fr = client1.idcard(by1,"front",new HashMap<String, String>());    //正面
        System.out.println("正面：" + jo_fr);
        System.out.println("正面：" + jo_fr.getJSONObject("words_result"));


        String  name= (String) jo_fr.getJSONObject("words_result").getJSONObject("姓名").get("words");
        System.out.println("正面：" + name);
        String  namepiny=chineseUtils.ToPinyin(name);
        String namepiny2 = null;

        System.out.println(namepiny2+"正面：******" + namepiny);
        ShopUser shopUser=shopUserService.queryCompare(namepiny);
        System.out.println("正面：******" + shopUser);

        int pl;
          if(shopUser == null){
            pl= 0;
            namepiny2=namepiny;
        }else{
              pl=shopUser.getPhone();
            pl++;
            System.out.println("///**********"+pl);
            namepiny2=namepiny+String.valueOf(pl);
            System.out.println("///**********"+pl);
        }


        Map map = new HashMap<String,String>();
        map.put("front", JSONChange.json2map(jo_fr.toString()));
        map.put("account",namepiny2);
        map.put("phone",pl);
        System.out.println("正面：" + map);
        return map;
    }

    /**
    * @Description: 查询员工信息
    **/
    @RequestMapping(value = "/queryShopUserYg",method = RequestMethod.GET)
    public List<ShopUser>  queryShopUserYg(ShopUser shopUser){
        return  shopUserService.queryShopUserYg(shopUser);
    }

    /**
    * @Description: 查询所有店铺名称及其id
    **/
    @ResponseBody
    @RequestMapping(value = "/queryShopYg",method = RequestMethod.GET)
    public Map queryShopYg(){
        List<Shop> shops=  shopService.queryShop();
        Map map=new HashMap();
        map.put("shops",shops);
        return  map;
    }

    /**
    * @Description: 查询所有职位信息
    **/
    @ResponseBody
    @RequestMapping(value = "/queryPostYg",method = RequestMethod.GET)
    public Map queryPostYg(){
        List<ShopPost> shopPosts=  shopPostService.queryShopPost1();
        Map map=new HashMap();
        map.put("shopPosts",shopPosts);
        return  map;
    }
    
    /**
    * @Description: 修改员工信息
    **/
    @RequestMapping(value = "/updateShopUserYg",method = RequestMethod.POST)
    public ResultBody  updateShopUserYg(String shopUserId,String mobile, String status, String workNumber, String postCode, String shopId, Date entryDate, String email, String remark) throws ParseException {
        ShopUser shopUser=new ShopUser();

        shopUser.setShopUserId(shopUserId);
        shopUser.setMobile(mobile);
        shopUser.setStatus(status);
        shopUser.setWorkNumber(workNumber);
        shopUser.setPostCode(postCode);
        shopUser.setShopId(shopId);
        shopUser.setEntryDate(entryDate);
        shopUser.setEmail(email);
        shopUser.setRemark(remark);
        System.out.println(shopUser.getWorkNumber()+"-----------");
        shopUserService.updateShopUserYg(shopUser);
        return   new ResultBody(CommonEnum.SUCCESS);
    }

    /**
    * @Description:  添加员工信息
    **/
    @RequestMapping("/saveShopUserYg")
    public ResultBody saveShopUserYg(ShopUser shopUser,String remark){
        System.out.println(shopUser.getAccount()+"--"+shopUser.getEntryDate());
        String userType="2";
        shopUser.setRemark(remark);
        String sex2=shopUser.getSex();
        if(sex2=="男" || sex2=="雄"){
            shopUser.setSex("1");
        }else if (sex2=="女" || sex2=="雌"){
            shopUser.setSex("2");
        }else{
            shopUser.setSex("3");
        }

        String shopUserId=Long.toString(System.currentTimeMillis());
        shopUser.setShopUserId(shopUserId);
        shopUser.setUserType(userType);
        shopUser.setSex(sex2);
        shopUser.setStatus("0");
        shopUser.setIsDel("0");
        shopUserService.saveShopUser(shopUser);
        return  new ResultBody(CommonEnum.SUCCESS);
    }



}
